package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.common.TestDsl

/**
 * @author flutterdash@qq.com
 * @since 2021/7/10 14:44
 */
class P383 : TestDsl {

    private fun canConstruct(ransomNote: String, magazine: String): Boolean {
        val cache = IntArray(26).apply {
            for (ch in magazine) {
                val pos = ch - 'a'
                this[pos]++
            }
        }

        for (ch in ransomNote) {
            val pos = ch - 'a'
            if (cache[pos] > 0) {
                cache[pos]--
            } else {
                return false
            }
        }

        return true
    }

    @Test
    fun test0() {
        canConstruct("a", "b") shouldBe false
        canConstruct("aa", "ab") shouldBe false
        canConstruct("aa", "aab") shouldBe true
    }

}
